﻿using Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dal
{
    public class RoleDal
    {
        public RoleDal(BabyfaceEntities _db)
        {
            db = _db;
        }
        public BabyfaceEntities db { get; set; }

        public int Add(TRole entity)
        {
            db.TRole.Add(entity);
            db.SaveChanges();

            return entity.TRoleId;
        }

        public int Edit(TRole entity)
        {
            db.TRole.Attach(entity);
            db.Entry(entity).State = EntityState.Modified;
            return db.SaveChanges();
        }

        public int Delete(int id)
        {
            var entity = new TRole { TRoleId = id };
            db.TRole.Attach(entity);
            db.TRole.Remove(entity);
            //db.Entry(entity).State = EntityState.Deleted;
            return db.SaveChanges();
        }

        public TRole GetById(int id)
        {
            var entity = db.TRole.Where(w => w.TRoleId == id).FirstOrDefault();
            return entity;
        }

        public IList<TRole> GetAll()
        {
            var list = db.TRole.ToList();
            return list;
        }


    }
}
